#include "baseshadowdialog.h"
#if (QT_VERSION <= QT_VERSION_CHECK(6, 0, 0))
#include <QDesktopWidget>
#else
#include <QScreen>
#endif
#include <QApplication>
#include <QVBoxLayout>
#ifdef WIN32
#include <WindowsX.h>
#include <Windows.h>
#endif
#include <QPainter>
#include <qmath.h>

BaseShadowDialog::BaseShadowDialog(QWidget *parent)
: QDialog(parent),
m_height(30)
{
	this->setWindowFlags(windowFlags()|Qt::FramelessWindowHint);

    this->setAttribute(Qt::WA_TranslucentBackground);

#if (QT_VERSION <= QT_VERSION_CHECK(6, 0, 0))
	QDesktopWidget *pDesktop = QApplication::desktop();
	m_rect = pDesktop->availableGeometry();
#else
    m_rect = window()->screen()->geometry();
#endif

    QHBoxLayout *pLayout = new QHBoxLayout(this);
	pLayout->setSpacing(0);
    pLayout->setContentsMargins(6, 6, 6, 6);
    
	this->setLayout(pLayout);
    
    m_shadow = new QGraphicsDropShadowEffect;
    this->setShadowRadius(6);
    this->setShadowColor("#3F000000");
    this->setShadowSize(QPointF(0,0));
}

BaseShadowDialog::~BaseShadowDialog()
{
    if(m_shadow)
    {
        delete m_shadow;
        m_shadow = nullptr;
    }
}

void BaseShadowDialog::setTitleHeight(int height)
{
	m_height = height;
}

void BaseShadowDialog::setMainWidget(QWidget *widget)
{
    if(!widget)
        return;
    
    this->layout()->addWidget(widget);
    if(widget)
        widget->setGraphicsEffect(m_shadow);
}

void BaseShadowDialog::removeMainWidget(QWidget *widget)
{
     if(!widget)
         return;

     this->layout()->removeWidget(widget);
}

void BaseShadowDialog::setShadowRadius(int radius)
{
    m_shadow->setBlurRadius(radius);
}

void BaseShadowDialog::setShadowSize(QPointF size)
{
    m_shadow->setOffset(size);
}

void BaseShadowDialog::setShadowColor(QColor color)
{
    m_shadow->setColor(color);
}

void BaseShadowDialog::moveCenter()
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
	QDesktopWidget *pDesktop = QApplication::desktop();
    this->move((pDesktop->width() - this->width()) / 2, (pDesktop->height() - this->height()) / 2);
#else
    this->move((window()->screen()->geometry().width() - this->width()) / 2, (window()->screen()->geometry().height() - this->height()) / 2);
#endif
}

void BaseShadowDialog::focusOutEvent(QFocusEvent* event)
{
    emit this->sigFocusOut();
}

